home *** CD-ROM | disk | FTP | other *** search
/ Pulps on CDRom / Pulps on CDROM.iso / comm.z / CntTool.js < prev    next >
Text File  |  1998-10-20  |  15KB  |  452 lines

  1. /* ==================================================================
  2. FILE:   CntTool.js
  3. DESCR:  Contents control library file for Netscape Help implementation.
  4. NOTES:  Requires Utility.js
  5. ================================================================== */
  6. var ASSERT = true
  7.  
  8. var contentsObj
  9.  
  10. var aDatasets      = new Array()
  11. var aTmpContainers = new Array()
  12. var aTmpItems      = new Array()
  13.  
  14. /*
  15. DESCR:   Contents class.
  16. PARAMS:  bgcolor  Background color for the Contents tool.
  17. RETURNS: 
  18. NOTES:   
  19. */
  20. function contents( bgcolor )
  21. {
  22.    //top.SystemFrame.trace( "contents construct" )
  23.    //alert( "contents construct" )
  24.  
  25.    this.bgcolor = bgcolor
  26.  
  27.    this.lastToggledContainerIndex
  28.  
  29.    this.bContainerToggle = false
  30.    this.bItemSelected    = false
  31.    this.currentDataset   = ""
  32.  
  33.    this.setDataset     = setDataset
  34.    this.updateTree     = updateTree
  35.    this.updateEntries  = updateEntries
  36.    this.writeDocument  = writeDocument
  37.    this.scrollDocument = scrollDocument
  38.    this.containerClick = containerClick
  39.    this.itemClick      = itemClick
  40.  
  41.    // Set global reference to this component.
  42.    contentsObj = this
  43.  
  44.    // Load data.
  45.    loadData()
  46. }
  47.  
  48.    /*
  49.    DESCR:   Selects a dataset for the tool.
  50.    PARAMS:  indexName  The name that indexes the dataset in the
  51.                        datasets array.
  52.    RETURNS: 
  53.    NOTES:   
  54.    */
  55.    function setDataset( indexName )
  56.    {
  57.       //alert( "setDataset, " + indexName )
  58.       //top.SystemFrame.trace( "contents setDataset()" )
  59.  
  60.       this.currentDataset = indexName
  61.    }
  62.  
  63.    /*
  64.    DESCR:   Updates the Contents tree.
  65.    PARAMS:  newURL  The current topic. Passing a URL expands its container
  66.             as needed and selects the item representing the URL. If updating
  67.             to just to toggle a container, pass null.
  68.    RETURNS: 
  69.    NOTES:   
  70.    */
  71.    function updateTree( newURL )
  72.    {
  73.       //top.SystemFrame.trace( "contents updateTree()" )
  74.       //alert( "updateTree" )
  75.  
  76.       assert( ( this.currentDataset != "" ), DATASET )
  77.  
  78.       // Write a background color to the hidden frame, and bind its events to
  79.       // the global handlers.
  80.       frames[ 0 ].document.open()
  81.       var html = "<BODY BGCOLOR = " + this.bgcolor + ">"
  82.       html += "<SCRIPT LANGUAGE = 'JavaScript1.2'>"
  83.       html += "top.bindDocEvts( document )"
  84.       html += "</SCRIPT></BODY>"
  85.       frames[ 0 ].document.write( html )
  86.       frames[ 0 ].document.close()
  87.  
  88.       // Bind hidden frame events to global event handlers.
  89.       top.bindDocEvts( frames[ 0 ].document )
  90.  
  91.       // Determine if this is an update just to toggle a container.
  92.       this.bContainerToggle = ( ( typeof( newURL ) == "undefined" ) ? true : false )
  93.  
  94.       // Update the entry objects.
  95.       if ( !this.bContainerToggle ) this.updateEntries( newURL )
  96.  
  97.       // Write the tree.
  98.       this.writeDocument()
  99.  
  100.       // Bind event handlers.
  101.       top.bindDocEvts( ContentsFrame.document )
  102.  
  103.       // Scroll the tree.
  104.       if ( this.bItemSelected ) this.scrollDocument()
  105.    }
  106.  
  107.    /*
  108.    DESCR:   Updates the tree entry objects.
  109.    PARAMS:  newURL  The URL from updateTree().
  110.    RETURNS: 
  111.    NOTES:  
  112.    */
  113.    function updateEntries( newURL )
  114.    {
  115.       //top.SystemFrame.trace( "contents updateEntries()" )
  116.       //alert("updateEntries()")
  117.  
  118.       var aTmpContainers = aDatasets[ this.currentDataset ]
  119.  
  120.       // Check each item...
  121.       var itemsSelected = 0  // Assert only.
  122.       this.bItemSelected = false
  123.       for ( var i = 0; i < aTmpContainers.length; i++ ) {
  124.          for ( var j = 0; j < aTmpContainers[ i ].aItems.length; j++ ) {
  125.             with ( aTmpContainers[ i ].aItems[ j ] ) {
  126.                
  127.                // ...and turn off any that are selected...
  128.                if ( bSelected ) bSelected = false
  129.  
  130.                // ...and select the specified item...
  131.                if ( URLsSansPathsAreSame( URL, newURL ) ) {
  132.  
  133.                   itemsSelected++
  134.                   assert( ( itemsSelected < 2 ), ITEMS_SELECTED )
  135.  
  136.                   bSelected = true
  137.                   this.bItemSelected = true
  138.  
  139.                   // ...making sure the selected item's container is expanded.
  140.                   aTmpContainers[ i ].bExpanded = true
  141.                }
  142.             }
  143.          }
  144.       }
  145.    }
  146.  
  147.    /*
  148.    DESCR:   Scrolls the tree.
  149.    PARAMS:  
  150.    RETURNS: 
  151.    NOTES:   If update is triggered outside of tool, we want to scroll as
  152.             needed to the selected item to show where user is in the tree.
  153.             Same for an item click, so the item is visible. For a container
  154.             click, we only want to scroll to the clicked container, since
  155.             the user is just looking for, or covering up, items.
  156.    */
  157.    function scrollDocument()
  158.    {
  159.       //top.SystemFrame.trace( "contents scrollDocument()" )
  160.       //alert("scrollDocument")
  161.  
  162.       // Ignore scrolling if content does not exceed window.
  163.       if ( frames[ 1 ].document.height < frames[ 1 ].innerHeight ) return
  164.  
  165.       // Calculate our best guess of pixel magnitude along the y axis of tree
  166.       // window for the item we want to make visible.
  167.       var aTmpContainers  = aDatasets[ this.currentDataset ]
  168.       var y               = 0
  169.       var containerHeight = 11
  170.       var itemHeight      = 11
  171.       var topMargin       = 4
  172.       var bottomMargin    = 4
  173.       var wrapLeading     = 4
  174.       var charPerLine     = 14
  175.             
  176.       // Tally pixels for entries.
  177.       for ( var i = 0; i < aTmpContainers.length; i++ ) {
  178.  
  179.          // Tally for the container. Tally must account for how
  180.          // many lines the entry takes up, including the spacing between
  181.          // lines that wrap.
  182.          var lines = Math.ceil( aTmpContainers[ i ].text.length / charPerLine )
  183.          y += topMargin +
  184.               ( containerHeight * lines ) +
  185.               ( wrapLeading * ( lines - 1 ) ) +
  186.               bottomMargin
  187.  
  188.          // If just toggling a container, tally only to the newly
  189.          // toggled container.
  190.          if ( this.bContainerToggle && this.lastToggledContainerIndex == i ) {
  191.             break
  192.          }
  193.  
  194.          // Tally items in opened containers.
  195.          if ( aTmpContainers[ i ].bExpanded ) {
  196.             for ( var j = 0; j < aTmpContainers[ i ].aItems.length; j++ ) {
  197.                
  198.                // Tally for the item.
  199.                var tmpItemLength = aTmpContainers[ i ].aItems[ j ].text.length
  200.                lines = Math.ceil( tmpItemLength / charPerLine )
  201.                y += topMargin +
  202.                     ( itemHeight * lines ) +
  203.                     ( wrapLeading * ( lines - 1 ) ) +
  204.                     bottomMargin
  205.  
  206.                // If updating for a newly selected item, stop all tallying
  207.                // when we get to the selected item.
  208.                if ( !this.bContainerToggle &&
  209.                     aTmpContainers[ i ].aItems[ j ].bSelected ) {
  210.                   i = ( aTmpContainers.length )
  211.                   break
  212.                }
  213.             }
  214.          }
  215.       }
  216.  
  217.       // Scroll to bring the entry to the middle of the window.
  218.       var frameHeight = frames[ 1 ].innerHeight
  219.       var center = ( frameHeight / 2 )
  220.       if ( y > center ) frames[ 1 ].scrollTo( 0, y - center )
  221.    }
  222.  
  223.    /*
  224.    DESCR:   Writes the tree document.
  225.    PARAMS:  
  226.    RETURNS: 
  227.    NOTES:   
  228.    */
  229.    function writeDocument()
  230.    {
  231.    
  232.  
  233.    
  234.       //top.SystemFrame.trace( "contents writeDocument()" )
  235.       //alert("writeDocument");
  236.  
  237.       var aTmpContainers = aDatasets[ this.currentDataset ];
  238.       var link;
  239.       var platForm = navigator.platform;
  240.       
  241.     if ((platForm == "Win32")) {
  242.       var html = "<HTML><HEAD>"
  243.       html += "<STYLE TYPE = 'text/javascript'>"
  244.       html += "classes.container.a.fontFamily = 'helvetica, arial';"
  245.       html += "classes.container.a.fontSize = '14px';"
  246.       html += "classes.container.a.fontWeight = 'bold';"
  247.       html += "classes.container.a.marginTop = '12';"
  248.       html += "classes.container.a.marginBottom = '4';"
  249.       html += "classes.container.a.color = '#000000';"
  250.       
  251.       html += "classes.unselectedItem.a.fontFamily = 'helvetica, arial';"
  252.       html += "classes.unselectedItem.a.fontSize = '14px';"
  253.       html += "classes.unselectedItem.a.marginLeft = '4';"
  254.       html += "classes.unselectedItem.a.marginTop = '8';"
  255.       html += "classes.unselectedItem.a.marginLeft = '8';"
  256.       html += "classes.unselectedItem.a.marginBottom = '4';"
  257.       html += "classes.unselectedItem.a.color = '#000066';"
  258.       
  259.       html += "classes.selectedItem.a.fontFamily = 'helvetica, arial';"
  260.       html += "classes.selectedItem.a.fontSize = '14px';"
  261.       html += "classes.selectedItem.a.fontWeight = 'bold';"
  262.       html += "classes.selectedItem.a.marginLeft = '4';"
  263.       html += "classes.selectedItem.a.marginTop = '8';"
  264.       html += "classes.selectedItem.a.marginLeft = '8';"
  265.       html += "classes.selectedItem.a.marginBottom = '4';"
  266.       html += "classes.selectedItem.a.color = '#000066';"
  267.       html += "classes.selectedItem.a.fontWeight = 'bold';"
  268.       html += "tags.a.textDecoration = 'none';"
  269.       html += "</STYLE>"
  270.                 
  271.     }
  272.                 
  273.     else if ((platForm == "MacPPC") || (platform == "Mac68k")) {
  274.                 
  275.       var html = "<HTML><HEAD>"
  276.       html += "<STYLE TYPE = 'text/javascript'>"
  277.       html += "tags.body.fontFamily = 'helvetica, arial';"
  278.       html += "tags.body.fontSize = '10px';"
  279.       html += "classes.container.a.fontFamily = 'helvetica, arial';"
  280.       html += "classes.container.a.fontSize = '12px';"
  281.       html += "classes.container.a.fontWeight = 'bold';"
  282.       html += "classes.container.a.marginTop = 12;"
  283.       html += "classes.container.a.marginBottom = 4;"
  284.       html += "classes.container.a.color = '#000000';"
  285.  
  286.       html += "classes.unselectedItem.a.fontFamily = 'helvetica, arial';"
  287.       html += "classes.unselectedItem.a.fontSize = '12px';"
  288.       html += "classes.unselectedItem.a.marginLeft = 4;"
  289.       html += "classes.unselectedItem.a.marginTop = 8;"
  290.       html += "classes.unselectedItem.a.marginLeft = 8;"
  291.       html += "classes.unselectedItem.a.marginBottom = 4;"
  292.       html += "classes.unselectedItem.a.color = '#000066';"
  293.  
  294.       html += "classes.selectedItem.a.fontFamily = 'helvetica, arial';"
  295.       html += "classes.selectedItem.a.fontSize = '12px';"
  296.       html += "classes.selectedItem.a.fontWeight = 'bold';"
  297.       html += "classes.selectedItem.a.marginLeft = 4;"
  298.       html += "classes.selectedItem.a.marginTop = 8;"
  299.       html += "classes.selectedItem.a.marginLeft = 8;"
  300.       html += "classes.selectedItem.a.marginBottom = 4;"
  301.       html += "classes.selectedItem.a.color = '#000066';"
  302.       html += "classes.selectedItem.a.fontWeight = 'bold';"
  303.       html += "tags.a.textDecoration = 'none';"
  304.       html += "</STYLE>"
  305.                 
  306.     }
  307.                 
  308.     else {
  309.                 
  310.       var html = "<HTML><HEAD>"
  311.       html += "<STYLE TYPE = 'text/javascript'>"
  312.       html += "tags.body.fontFamily = 'helvetica, arial';"
  313.       html += "tags.body.fontSize = '10pt';"
  314.       html += "classes.container.a.fontFamily = 'helvetica, arial';"
  315.       html += "classes.container.a.fontSize = '12pt';"
  316.       html += "classes.container.a.fontWeight = 'bold';"
  317.       html += "classes.container.a.marginTop = 12;"
  318.       html += "classes.container.a.marginBottom = 4;"
  319.       html += "classes.container.a.color = '#000000';"
  320.  
  321.       html += "classes.unselectedItem.a.fontFamily = 'helvetica, arial';"
  322.       html += "classes.unselectedItem.a.fontSize = '10pt';"
  323.       html += "classes.unselectedItem.a.marginLeft = 4;"
  324.       html += "classes.unselectedItem.a.marginTop = 8;"
  325.       html += "classes.unselectedItem.a.marginLeft = 8;"
  326.       html += "classes.unselectedItem.a.marginBottom = 4;"
  327.       html += "classes.unselectedItem.a.color = '#000066';"
  328.  
  329.       html += "classes.selectedItem.a.fontFamily = 'helvetica, arial';"
  330.       html += "classes.selectedItem.a.fontSize = '10pt';"
  331.       html += "classes.selectedItem.a.fontWeight = 'bold';"
  332.       html += "classes.selectedItem.a.marginLeft = 4;"
  333.       html += "classes.selectedItem.a.marginTop = 8;"
  334.       html += "classes.selectedItem.a.marginLeft = 8;"
  335.       html += "classes.selectedItem.a.marginBottom = 4;"
  336.       html += "classes.selectedItem.a.color = '#000066';"
  337.       html += "classes.selectedItem.a.fontWeight = 'bold';"
  338.       html += "tags.a.textDecoration = 'none';"
  339.       html += "</STYLE>"
  340.                 
  341.     }
  342.  
  343.  
  344.  
  345.  
  346.       html += "</HEAD><BODY BGCOLOR = " + this.bgcolor +
  347.               " LINK = '#000000' ALINK = '#000000' VLINK = '#000000'>"
  348.    
  349.    
  350.  
  351.  
  352.       for ( var i = 0; i < aTmpContainers.length; i++ ) {
  353.  
  354.          link = "\"javascript:parent.contentsObj.containerClick('" + this.currentDataset + "', " + i + ")\""
  355.          html += "<A CLASS = 'container' HREF = " + link + ">" + aTmpContainers[ i ].text + "</A>"
  356.  
  357.          if ( aTmpContainers[ i ].bExpanded ) {
  358.             for ( var j = 0; j < aTmpContainers[ i ].aItems.length; j++ ) {
  359.                with ( aTmpContainers[ i ].aItems[ j ] ) {
  360.  
  361.                   var ssClass = ( bSelected ? "selectedItem" : "unselectedItem" )
  362.                   link = "\"javascript:parent.contentsObj.itemClick('" + this.currentDataset + "', " + i + ", " + j + ")\""
  363.                   html += "<A CLASS = '" + ssClass + "' HREF = " + link + ">" + text + "</A>"
  364.                }
  365.             }
  366.          }
  367.       }
  368.  
  369.       html += "</BODY></HTML>"
  370.  
  371.       with ( frames[ 1 ].document ) {
  372.          open()
  373.          write( html )
  374.          close()
  375.       }
  376.    }
  377.  
  378.    /*
  379.    DESCR:   Container object click "event."
  380.    PARAMS:  datasetIndex    The dataset index.
  381.             containerIndex  The container index.
  382.    RETURNS: 
  383.    NOTES:   
  384.    */
  385.    function containerClick( datasetIndex, containerIndex )
  386.    {
  387.       // Toggle the container's state.
  388.       var obj = aDatasets[ datasetIndex ][ containerIndex ]
  389.       obj.bExpanded = ( obj.bExpanded ? false : true )
  390.       this.lastToggledContainerIndex = containerIndex
  391.             
  392.       // Update the tree.
  393.       this.updateTree()
  394.    }
  395.  
  396.    
  397.    /*
  398.    DESCR:   Item object click "event."
  399.    PARAMS:  datasetIndex    The dataset index.
  400.             containerIndex  The container index.
  401.             itemIndex       The item index.
  402.    RETURNS: 
  403.    NOTES:   
  404.    */
  405.    function itemClick( datasetIndex, containerIndex, itemIndex )
  406.    {
  407.       // Notify component owner of selection, and update tree on success.
  408.       var obj = aDatasets[ datasetIndex ][ containerIndex ].aItems[ itemIndex ]
  409.       
  410.       // Load the topic (location doesn't matter since it's a nethelp URL.
  411.       location = obj.netHelpURL
  412.    }
  413.  
  414.  
  415. // End class definition: contents.
  416.  
  417. /*
  418. DESCR:   Container class.
  419. PARAMS:  text    The container's text.
  420.          aItems  Array of item objects.
  421. RETURNS: 
  422. NOTES:   
  423. */
  424. function container( text, aItems )
  425. {
  426.    this.text     = text
  427.    this.aItems   = aItems
  428.  
  429.    this.bExpanded = false
  430. }
  431.  
  432. // End class definition: container.
  433.  
  434. /*
  435. DESCR:   Item class.
  436. PARAMS:  text        The item's text.
  437.          URL         The URL in non-NetHelp form.
  438.          netHelpURL  The URL in NetHelp form.
  439. RETURNS: 
  440. NOTES:   
  441. */
  442. function item( text, URL, netHelpURL )
  443. {
  444.    this.text       = text
  445.    this.URL        = URL
  446.    this.netHelpURL = netHelpURL
  447.  
  448.    this.bSelected = false
  449. }
  450.  
  451. // End class definition: item.
  452.